package edu.ynu.se.xiecheng.achitectureclass.project.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import edu.ynu.se.xiecheng.achitectureclass.project.dao.AirHumidityDataDAO;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.AirHumidityData;
import edu.ynu.se.xiecheng.achitectureclass.project.po.vo.EnvironmentVO;
import edu.ynu.se.xiecheng.achitectureclass.project.service.AirHumidityDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ah")
@Api(tags = "空气湿度数据管理")
public class AirHumidityDataController extends LogicController<AirHumidityDataService, AirHumidityDataDAO, AirHumidityData, Long> {

    public AirHumidityDataController(AirHumidityDataService ls) {
        super(ls);
    }

    @ApiOperation("从传感器API获取空气相对湿度数据并存储到数据库")
    @PostMapping("/fetch")
    public  ResponseEntity<String> fetchAndStorePHData() {
        try {
            service.fetchAndSaveAirHumidityData(); // 调用 Service 层方法
            return ResponseEntity.ok("空气相对湿度数据成功获取并存储！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.internalServerError()
                    .body("获取并存空气相对湿度数据数据时发生错误：" + e.getMessage());
        }
    }

    @GetMapping("/current")
    @ApiOperation("获取实时的空气湿度数据")
    public ResponseEntity<EnvironmentVO> getCurrentAirTemperature() {
        try {
            EnvironmentVO data = service.getCurrentAirTemperature();
            return ResponseEntity.ok(data);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}
